package com.bobo.controller;

import com.bobo.model.InsertSortData;
import com.bobo.view.AlgoFrame;
import com.bobo.units.AlgoVisUnit;

import java.awt.*;

/**
 * @Author: bobobo
 * @Date: 2019/9/3 11:34
 * @Version：1.0
 */
public class AlgoVisualizer {
	private static int DELAY = 200;
    // 创建自己的数据
    private InsertSortData data;  //数据
    private AlgoFrame frame;   //视图

    public AlgoVisualizer(int sceneWidth, int sceneHeight,int N, InsertSortData.Type dataType){
        //初始化自己的数据
        //初始化数据
        data = new InsertSortData(N, sceneHeight, dataType);

        //初始化视图
        // 事件队列 创建新线程===事件分发线程
        EventQueue.invokeLater(() -> {
            frame = new AlgoFrame("插入排序模拟",sceneWidth,sceneHeight);

            new Thread(() -> {
                run();
            }).start();
        });
    }

    public AlgoVisualizer(int sceneWidth, int sceneHeight,int N) {
        this(sceneWidth, sceneHeight, N, InsertSortData.Type.Default);
    }

        //动画逻辑
    private void run(){

        setData(0,-1);
        // 编写自己的动画逻辑
        for ( int i = 0;i < data.N(); i ++ ){
            for ( int j = i ; j > 0 && data.get(j) < data.get(j-1); j -- ){
                data.swap(j,j-1);
                setData(i+1,j-1);
            }
        }
        setData(data.N(),-1);
    }

    private void setData(int orderedIndex,int currentindex){

        data.orderedIndex = orderedIndex;
        data.currentIndex = currentindex;

        frame.render(data);
        AlgoVisUnit.pause(DELAY);
    }

}
